home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / online / source / c / compilers / Bob 1.5.sit.hqx / Bob 1.5 / Bob.h < prev    next >
Text File  |  1993-10-02  |  11KB  |  342 lines

  1. /* bob.h - bob definitions */
  2. /*
  3.     Copyright (c) 1991, by David Michael Betz
  4.     All rights reserved
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <ctype.h>
  11.  
  12. /* limits */
  13. #define TKNSIZE        50    /* maximum token size */
  14. #define SMAX        500    /* runtime stack size */
  15.  
  16. /* useful definitions */
  17. #define TRUE        1
  18. #define FALSE        0
  19.  
  20. /* argument check macros */
  21. #define argcount(n,cnt)    { if ((n) != (cnt)) wrongcnt(n,cnt); }
  22.  
  23. /* stack manipulation macros */
  24. #define check(n)     { if (sp - (n) < stkbase) stackover(); }
  25. #define chktype(o,t)     { if (sp[o].v_type != t) badtype(o,t); }
  26. #define push(x,t,f)     (--sp, sp->v_type = (t), sp->v.f = (x))
  27. #define push_integer(x)     push(x,DT_INTEGER,v_integer)
  28. #define push_string(x)     push(x,DT_STRING,v_string)
  29. #define push_class(x)     push(x,DT_CLASS,v_class)
  30. #define push_object(x)     push(x,DT_OBJECT,v_object)
  31. #define push_bytecode(x) push(x,DT_BYTECODE,v_vector)
  32. #define push_var(x)     push(x,DT_VAR,v_var)
  33. #define push_nil()     (--sp, sp->v_type = DT_NIL)
  34.  
  35. /* macros to set values */
  36. #define set(s,x,t,f)        ((s)->v.f = (x), (s)->v_type = (t))
  37. #define set_integer(s,x)    set(s,x,DT_INTEGER,v_integer)
  38. #define set_class(s,x)        set(s,x,DT_CLASS,v_class)
  39. #define set_object(s,x)     set(s,x,DT_OBJECT,v_object)
  40. #define set_code(s,x)       set(s,x,DT_CODE,v_code)
  41. #define set_bytecode(s,x)   set(s,x,DT_BYTECODE,v_vector)
  42. #define set_dictionary(s,x) set(s,x,DT_DICTIONARY,v_dictionary)
  43. #define set_var(s,x)        set(s,x,DT_VAR,v_var)
  44. #define set_string(s,x)        set(s,x,DT_STRING,v_string)
  45. #define set_vector(s,x)        set(s,x,DT_VECTOR,v_vector)
  46. #define set_iostream(s,x)   set(s,x,DT_IOSTREAM,v_iostream)
  47. #define set_nil(s)        ((s)->v_type = DT_NIL)
  48.  
  49. /* value field access macros */
  50. #define valtype(x)        ((x)->v_type)
  51. #define isnil(x)        ((x)->v_type == DT_NIL)
  52.  
  53. /* class field access macros */
  54. #define claddr(x)        ((x)->v.v_class)
  55. #define clgetname(x)        (&claddr(x)->cl_name)
  56. #define clgetbase(x)        (&claddr(x)->cl_base)
  57. #define clgetmembers(x)        (&claddr(x)->cl_members)
  58. #define clgetfunctions(x)    (&claddr(x)->cl_functions)
  59. #define clgetsize(x)        (claddr(x)->cl_size)
  60.  
  61. /* object field access macros */
  62. #define objaddr(x)        ((x)->v.v_object)
  63. #define objgetclass(x)        (&objaddr(x)->obj_class)
  64. #define objgetmember(x,i)    (&objaddr(x)->obj_members[i])
  65. #define objsetmember(x,i,v)    (objaddr(x)->obj_members[i] = (v))
  66.  
  67. /* vector field access macros */
  68. #define vecaddr(x)        ((x)->v.v_vector)
  69. #define vecgetsize(x)        (vecaddr(x)->vec_size)
  70. #define vecgetelement(x,i)    (&vecaddr(x)->vec_data[i])
  71. #define vecsetelement(x,i,v)    (vecaddr(x)->vec_data[i] = (v))
  72.  
  73. /* string field access macros */
  74. #define straddr(x)        ((x)->v.v_string)
  75. #define strgetsize(x)        (straddr(x)->str_size)
  76. #define strgetdata(x)        (straddr(x)->str_data)
  77.  
  78. /* dictionary field access macros */
  79. #define diaddr(x)        ((x)->v.v_dictionary)
  80. #define digetclass(x)        (&diaddr(x)->di_class)
  81. #define digetcontents(x)    (&diaddr(x)->di_contents)
  82.  
  83. /* dictionary entry field access macros */
  84. #define deaddr(x)        ((x)->v.v_var)
  85. #define degetdictionary(x)    (&deaddr(x)->de_dictionary)
  86. #define degetkey(x)        (&deaddr(x)->de_key)
  87. #define degetvalue(x)        (&deaddr(x)->de_value)
  88. #define degetnext(x)        (&deaddr(x)->de_next)
  89. #define degettype(x)        (deaddr(x)->de_type)
  90.  
  91. /* i/o stream access macros */
  92. #define ios_t(x)        ((x)->v.v_iostream->ios_dispatch)
  93. #define ios_d(x)        ((x)->v.v_iostream->ios_data)
  94. #define iosclose(x)        ((*ios_t(x)->iod_close)(ios_d(x)))
  95. #define iosgetc(x)        ((*ios_t(x)->iod_getc)(ios_d(x)))
  96. #define iosputc(c,x)        ((*ios_t(x)->iod_putc)((c),ios_d(x)))
  97. #define iosputs(s,x)        ((*ios_t(x)->iod_puts)((s),ios_d(x)))
  98.  
  99. /* value descriptor structure */
  100. typedef struct value {
  101.   int v_type;                /* data type */
  102.   union {                /* value */
  103.     struct class *v_class;        /*   class (in heap) */
  104.     struct object *v_object;        /*   object (in heap) */
  105.     struct vector *v_vector;        /*   vector (in heap) */
  106.     struct string *v_string;        /*   string (in heap) */
  107.     struct dictionary *v_dictionary;    /*   dictionary (in heap) */
  108.     struct dict_entry *v_var;        /*   variable (in heap) */
  109. #ifdef __STDC__
  110.     int (*v_code)(int);            /*   code for built-in function */
  111. #else
  112.     int (*v_code)();            /*   code for built-in function */
  113. #endif
  114.     long v_integer;            /*   integer */
  115.     struct iostream *v_iostream;    /*   i/o stream (in heap) */
  116.     struct hdr *v_hdr;            /*   (used by garbage collector) */
  117.     struct value *v_chain;        /*   (used by garbage collector) */
  118.   } v;
  119. } VALUE;
  120.  
  121. typedef struct hdr {
  122.     char hdr_type;
  123.     char hdr_flags;
  124.     VALUE *hdr_chain;
  125. } HDR;
  126.  
  127. typedef struct class {
  128.     HDR cl_hdr;
  129.     VALUE cl_name;
  130.     VALUE cl_base;
  131.     VALUE cl_members;
  132.     VALUE cl_functions;
  133.     int cl_size;
  134. } CLASS;
  135.  
  136. typedef struct object {
  137.     HDR obj_hdr;
  138.     VALUE obj_class;
  139.     VALUE obj_members[1];
  140. } OBJECT;
  141.  
  142. typedef struct vector {
  143.     HDR vec_hdr;
  144.     int vec_size;
  145.     VALUE vec_data[1];
  146. } VECTOR;
  147.  
  148. typedef struct string {
  149.     HDR str_hdr;
  150.     int str_size;
  151.     char str_data[1];
  152. } STRING;
  153.  
  154. typedef struct dictionary {
  155.     HDR di_hdr;
  156.     VALUE di_class;
  157.     VALUE di_contents;
  158. } DICTIONARY;
  159.  
  160. /* dictionary entry structure */
  161. typedef struct dict_entry {
  162.     HDR de_hdr;
  163.     VALUE de_dictionary;    /* backpointer to dictionary */
  164.     VALUE de_key;        /* symbol name */
  165.     int de_type;        /* symbol type */
  166.     VALUE de_value;        /* symbol value */
  167.     VALUE de_next;        /* next entry */
  168. } DICT_ENTRY;
  169.  
  170. /* i/o stream dispatch structure */
  171. typedef struct iodispatch {
  172. #ifdef __STDC__
  173.     int (*iod_close)(void *);
  174.     int (*iod_getc)(void *);
  175.     int (*iod_putc)(int,void *);
  176.     int (*iod_puts)(char *,void *);
  177. #else
  178.     int (*iod_close)();
  179.     int (*iod_getc)();
  180.     int (*iod_putc)();
  181.     int (*iod_puts)();
  182. #endif
  183. } IODISPATCH;
  184.  
  185. /* i/o stream structure */
  186. typedef struct iostream {
  187.     HDR ios_hdr;
  188.     IODISPATCH *ios_dispatch;
  189.     void *ios_data;
  190. } IOSTREAM;
  191.  
  192. /* symbol types */
  193. #define ST_CLASS    1    /* class definition */
  194. #define ST_DATA        2    /* data member */
  195. #define ST_SDATA    3    /* static data member */
  196. #define ST_FUNCTION    4    /* function member */
  197. #define ST_SFUNCTION    5    /* static function member */
  198.  
  199. /* data types */
  200. #define _DTMIN        0
  201. #define DT_NIL        0
  202. #define DT_CLASS    1
  203. #define DT_OBJECT    2
  204. #define DT_VECTOR    3
  205. #define DT_INTEGER    4
  206. #define DT_STRING    5
  207. #define DT_BYTECODE    6
  208. #define DT_CODE        7
  209. #define DT_DICTIONARY    8
  210. #define DT_VAR        9
  211. #define DT_IOSTREAM    10
  212. #define _DTMAX        10
  213.  
  214. /* opcodes */
  215. #define OP_BRT        0x01    /* branch on true */
  216. #define OP_BRF        0x02    /* branch on false */
  217. #define OP_BR        0x03    /* branch unconditionally */
  218. #define OP_NIL        0x04    /* load top of stack with nil */
  219. #define OP_PUSH        0x05    /* push nil onto stack */
  220. #define OP_NOT        0x06    /* logical negate top of stack */
  221. #define OP_NEG        0x07    /* negate top of stack */
  222. #define OP_ADD        0x08    /* add top two stack entries */
  223. #define OP_SUB        0x09    /* subtract top two stack entries */
  224. #define OP_MUL        0x0A    /* multiply top two stack entries */
  225. #define OP_DIV        0x0B    /* divide top two stack entries */
  226. #define OP_REM        0x0C    /* remainder of top two stack entries */
  227. #define OP_BAND        0x0D    /* bitwise and of top two stack entries */
  228. #define OP_BOR        0x0E    /* bitwise or of top two stack entries */
  229. #define OP_XOR        0x0F    /* bitwise xor of top two stack entries */
  230. #define OP_BNOT        0x10    /* bitwise not of top two stack entries */
  231. #define OP_SHL        0x11    /* shift left top two stack entries */
  232. #define OP_SHR        0x12    /* shift right top two stack entries */
  233. #define OP_LT        0x13    /* less than */
  234. #define OP_LE        0x14    /* less than or equal to */
  235. #define OP_EQ        0x15    /* equal to */
  236. #define OP_NE        0x16    /* not equal to */
  237. #define OP_GE        0x17    /* greater than or equal to */
  238. #define OP_GT        0x18    /* greater than */
  239. #define OP_INC        0x19    /* increment */
  240. #define OP_DEC        0x1A    /* decrement */
  241. #define OP_LIT        0x1B    /* load literal */
  242. #define OP_RETURN    0x1C    /* return from interpreter */
  243. #define OP_CALL        0x1D    /* call a function */
  244. #define OP_REF        0x1E    /* load a variable value */
  245. #define OP_SET        0x1F    /* set the value of a variable */
  246. #define OP_VREF        0x20    /* load a vector element */
  247. #define OP_VSET        0x21    /* set a vector element */
  248. #define OP_MREF        0x22    /* load a member variable value */
  249. #define OP_MSET        0x23    /* set a member variable */
  250. #define OP_AREF        0x24    /* load an argument value */
  251. #define OP_ASET        0x25    /* set an argument value */
  252. #define OP_TREF        0x26    /* load a temporary variable value */
  253. #define OP_TSET        0x27    /* set a temporary variable */
  254. #define OP_TSPACE    0x28    /* allocate temporary variable space */
  255. #define OP_SEND        0x29    /* send a message to an object */
  256. #define OP_DUP2        0x2A    /* duplicate top two elements on the stack */
  257. #define OP_NEW        0x2B    /* create a new class object */
  258.  
  259. /* external variables */
  260. extern VALUE *stkbase,*sp,*fp,*stktop;
  261. extern VALUE nil;
  262.  
  263. /* external routines */
  264. #ifdef __STDC__
  265.  
  266. /* standard stuff */
  267. /*
  268. char *calloc(int,int);
  269. void free(char *);
  270. char *strcpy(char *,char *);
  271. char *strncpy(char *,char *,int);
  272. char *strchr(char *,int);
  273. int strcmp(char *,char *);
  274. int strncmp(char *,char *,int);
  275. int strlen(char *);
  276. void *memcpy(void *,void *,int);
  277. int system(char *);
  278. */
  279.  
  280. /* bobcom.c */
  281. int init_compiler(void);
  282. void mark_compiler(void);
  283. int compile_definitions(int (*getcf)(),void *getcd);
  284.  
  285. /* bob.c */
  286. void info(char *fmt,...);
  287. void error(char *fmt,...);
  288. void osputs(char *str);
  289.  
  290. /* bobscn.c */
  291. int init_scanner(int (*gf)(void *),void *gd);
  292. int token(void);
  293. int stoken(int tkn);
  294. char *tkn_name(int tkn);
  295. int parse_error(char *msg);
  296.  
  297. /* bobint.c */
  298. int execute(char *name);
  299. int start_call(char *name);
  300. int start_send(OBJECT *obj,char *selector);
  301. int execute_call(int argc);
  302. void badtype(int off,int type);
  303. void stackover(void);
  304.  
  305. /* bobdbg.c */
  306. int decode_procedure(VALUE *code);
  307. int decode_instruction(VALUE *code,int lc);
  308.  
  309. /* bobfcn.c */
  310. void init_functions(void);
  311. void add_function(char *name,int (*fcn)());
  312. int print1(VALUE *ios,int qflag,VALUE *val);
  313. void wrongcnt(int n,int cnt);
  314.  
  315. /* bobmem.c */
  316. int initialize(int smax);
  317. DICT_ENTRY *addentry(VALUE *dict,char *key,int type);
  318. DICT_ENTRY *findentry(VALUE *dict,char *key);
  319. STRING *makestring(char *str);
  320. char *getcstring(char *buf,int max,VALUE *str);
  321. STRING *newstring(int n);
  322. OBJECT *newobject(VALUE *class);
  323. VECTOR *newvector(int n);
  324. CLASS *newclass(char *name,VALUE *base);
  325. DICTIONARY *newdictionary(VALUE *class);
  326. IOSTREAM *newiostream(IODISPATCH *iod,void *data);
  327. void gc(void);
  328. void mark(VALUE *val);
  329.  
  330. #else
  331. extern CLASS *newclass();
  332. extern OBJECT *newobject();
  333. extern VECTOR *newvector();
  334. extern STRING *newstring();
  335. extern STRING *makestring();
  336. extern DICTIONARY *newdictionary();
  337. extern IOSTREAM *newiostream();
  338. extern DICT_ENTRY *findentry();
  339. extern DICT_ENTRY *addentry();
  340. extern char *getcstring();
  341. #endif
  342.